//
//  TextTableViewController.m
//  简单记事本
//
//  Created by 胡先剑 on 16/3/8.
//  Copyright © 2016年 胡先剑. All rights reserved.
//

#import "TextTableViewController.h"
#import "AddViewController.h"
#import "NetManager.h"

@interface TextTableViewController ()<AddViewControllerDelegate>
//保存数据的数组
@property (nonatomic,strong) NSMutableArray *list;

//记录选中了哪个单元格
@property (nonatomic,assign) int num;

@end

@implementation TextTableViewController

#pragma mark - 视图加载
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self searchNote];
}

#pragma mark - 获取记事本数据
- (void)searchNote
{
    [NetManager searchWithBlock:^(NSArray *arr)
     {
         self.list = [[NSMutableArray alloc] init];
         [self.list addObjectsFromArray:arr];
         [self.tableView reloadData];
     }];
}

#pragma mark - 设置表格行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.list.count;
}

#pragma mark - 设置表格内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    
    NSDictionary *dic = self.list[indexPath.row];
    
    //显示内容
    cell.textLabel.text = dic[@"Content"];
    
    //显示时间
    cell.detailTextLabel.text = dic[@"CDate"];
    
    return cell;
}

#pragma mark - 将要选中表格某行触发的方法
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    //保存选中了哪个单元格
    self.num = indexPath.row;
    
    return indexPath;
}

#pragma mark - 沿连线触发的方法
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    AddViewController *textViewController = segue.destinationViewController;
    
    //点击添加按钮的连线
    if ([segue.identifier isEqualToString:@"add"])
    {
        textViewController.delegate = self;
    }
    
    //点击单元格的连线
    if ([segue.identifier isEqualToString:@"cell"])
    {
        NSDictionary *dic = self.list[self.num];
        textViewController.content = dic[@"Content"];
    }
}

#pragma mark - 添加页的协议方法
- (void)Content:(NSString *)content andDate:(NSString *)date
{
    //添加记事本
    [NetManager addWithContent:content andDate:date andBlock:^
    {
        [self searchNote];
    }];
}

#pragma mark - 点击编辑时触发的事件
- (IBAction)delete:(UIBarButtonItem *)sender
{
    //将表格设为可编辑状态
    [self.tableView setEditing:!self.tableView.editing animated:YES];
    [self.navigationItem.rightBarButtonItem setTitle:self.tableView.editing?@"完成":@"编辑"];
}

#pragma mark - 当表格处于编辑状态时，单元格的样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

#pragma mark - 提供删除的方法
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    //删除内容
    NSDictionary *dic = self.list[indexPath.row];
    [NetManager removeWithNoteId:dic[@"ID"] andBlock:^
    {
        [self.list removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }];
}

@end
